package com.haochang.chunkvod.view.vod.flavor;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.model.order.Flavor;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 *@author qintao
 *created at 2016/8/26 12:16
 * 口味选择view，自适应换行
 */

public class FlavorSelectView extends ViewGroup
{
	private static final int PADDING_HOR = 10;//水平方向padding
	private static final int PADDING_VERTICAL = 5;//垂直方向padding
	private static final int SIDE_MARGIN = 10;//左右间距

	private Context context;
	private List<FlavorItem> viewList = new ArrayList<FlavorItem>();
	private int currentSelectedPos = -1;
	private OnTabChangedListener changedListener;

	public FlavorSelectView(Context context)
	{
		super(context);
		this.context = context;
	}

	public FlavorSelectView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		this.context = context;
	}

	public FlavorSelectView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		this.context = context;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b)
	{
		int childCount = getChildCount();
		int autualWidth = r - l;
		int x = SIDE_MARGIN;// 横坐标开始
		int y = 0;//纵坐标开始
		int rows = 1;
		for (int i = 0; i < childCount; i++)
		{
			View view = getChildAt(i);
			int width = view.getMeasuredWidth();
			int height = view.getMeasuredHeight();
			x += width;
			if (x > autualWidth)
			{
				x = width + SIDE_MARGIN;
				rows++;
			}
			y = rows * height;
			if (i == 0)
			{
				view.layout(x - width, y - height, x, y);
			} else
			{
				view.layout(x - width, y - height, x, y);
			}
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		int x = 0;//横坐标
		int y = 0;//纵坐标
		int rows = 1;//总行数
		int specWidth = MeasureSpec.getSize(widthMeasureSpec);
		int actualWidth = specWidth - SIDE_MARGIN * 2;//实际宽度
		int childCount = getChildCount();
		for (int index = 0; index < childCount; index++)
		{
			View child = getChildAt(index);
			child.setPadding(PADDING_HOR, PADDING_VERTICAL, PADDING_HOR, PADDING_VERTICAL);
			child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
			int width = child.getMeasuredWidth();
			int height = child.getMeasuredHeight();
			x += width;
			if (x > actualWidth)
			{//换行
				x = width;
				rows++;
			}
			y = rows * height;
		}
		setMeasuredDimension(actualWidth, y);
	}

	public void addTab(Flavor info)
	{
		int total = this.getChildCount();
		String menuId = info.getFlavorId();
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		FlavorItem menuView = new FlavorItem(context);
		menuView.setLayoutParams(params);
		menuView.setTag(total);
		menuView.setTag(R.id.menu_value_id, menuId);
		menuView.setTag(R.id.menu_value_name, info.getFlavorName());
		menuView.setText(info.getFlavorName());
		this.addView(menuView);
		menuView.setOnClickListener(clickListener);
		this.viewList.add(menuView);
		menuView.setSelected(false);
	}

	public void setOnTabChangedListener(OnTabChangedListener changedListener)
	{
		this.changedListener = changedListener;
	}

	private OnBaseClickListener clickListener = new OnBaseClickListener()
	{

		@Override
		public void onBaseClick(View v)
		{
			int pos = (Integer) v.getTag();
			String menuId = (String) v.getTag(R.id.menu_value_id);
			String name = (String) v.getTag(R.id.menu_value_name);
			setSelectedTable(pos, menuId, name);
		}
	};

	public boolean setSelectedTable(int position, String menuId, String name)
	{
		if (currentSelectedPos != position && null != this.viewList && this.viewList.size() > position)
		{
			int oldPos = currentSelectedPos;
			currentSelectedPos = position;

			for (int i = 0; i < this.viewList.size(); i++)
			{
				if (i == position)
					this.viewList.get(i).setSelected(true);
				else
					this.viewList.get(i).setSelected(false);
			}

			if (changedListener != null)
			{
				changedListener.onTabChanged(oldPos, currentSelectedPos, menuId, name);
			}
			return true;
		} else if (currentSelectedPos == position)
		{
			this.viewList.get(currentSelectedPos).setSelected(false);
			if (changedListener != null)
			{
				changedListener.onTabChanged(currentSelectedPos, currentSelectedPos, menuId, name);
			}
			currentSelectedPos = -1;
		}
		return false;
	}

	public interface OnTabChangedListener
	{
		void onTabChanged(int last, int current, String menuId, String name);
	}
}
